package com.main.common;

import com.main.exception.TooFrequentRequestException;
import lombok.Data;
import org.springframework.http.HttpStatus;

@Data
public class Response<T> {
    private int status;
    private String message;
    private T data;

    // 成功响应
    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setStatus(HttpStatus.OK.value());
        response.setMessage("成功");
        response.setData(data);
        return response;
    }

    // 失败响应
    public static <T> Response<T> failure(HttpStatus status, Exception e) {
        Response<T> response = new Response<>();
        response.setStatus(status.value());
        response.setMessage(e.getMessage());
        response.setData(null);
        return response;
    }

    // 从状态码和消息构建失败响应
    public static <T> Response<T> failure(HttpStatus status, String message) {
        Response<T> response = new Response<>();
        response.setStatus(status.value());
        response.setMessage(message);
        response.setData(null);
        return response;
    }

}
